package com.lostandfound.dao.impl;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.lostandfound.dao.UserDao;
import com.lostandfound.entity.User;

@Repository
public class UserDaoImpl implements UserDao{
	
	private Log logger = LogFactory.getLog(this.getClass());

	@Autowired
	private SessionFactory sessionFactory;
	
	@Override
	public Serializable save(User obj) {
		logger.info("注册了一个用户");
		return sessionFactory.getCurrentSession().save(obj);
	}

	@Override
	public void update(User obj) {
		logger.info("用户修改了个人信息");
		sessionFactory.getCurrentSession().update(obj);
	}

	@Override
	public void delete(User obj) {
		logger.info("删除了一个用户");
		sessionFactory.getCurrentSession().delete(obj);
	}

	@Override
	public User selectByPrimary(Long id) {
		
		return (User) sessionFactory.getCurrentSession().get(User.class, id);
	}

	@Override
	public User selectByUserCode(String userCode) {
		String sql = "from User u where (u.status = '0' or u.status = '1') and u.userCode = :userCode ";
		User user = (User) sessionFactory.getCurrentSession().createQuery(sql)
								.setParameter("userCode", userCode).uniqueResult();
		logger.info("用户执行登录" + userCode);
		return user;
	}

	@SuppressWarnings("rawtypes")
	@Override
	public List listUserByPage(Map<String, Object> pagemap) {
		
		Session session = sessionFactory.getCurrentSession();
		String hql = "from User u where (u.status = '0' or u.status = '1') ";
		if(pagemap != null){
			if(pagemap.get("type") != null && !((String)pagemap.get("type")).equals("")){
				hql = hql + "and u.type = '"
						+(String)pagemap.get("type")+ "' ";
			}
			if(pagemap.get("userCode") != null && !((String)pagemap.get("userCode")).equals("")){
				hql = hql + "and u.userCode like '" + (String)pagemap.get("userCode") + "%' ";
			}
			if(pagemap.get("userName") != null && !((String)pagemap.get("userName")).equals("")){
				hql = hql + "and u.userName like '" + (String)pagemap.get("userName") + "%' ";
			}
			if(pagemap.get("mindate") != null && 
					!pagemap.get("mindate").equals("") && 
					pagemap.get("maxdate") != null && 
					!pagemap.get("maxdate").equals("") ){
				hql = hql + "and u.createDate >= '"
						+ pagemap.get("mindate") +"' and u.createDate <= '"
						+ pagemap.get("maxdate") + "'";
			}
		}
		int page = Integer.parseInt((String)pagemap.get("page"));
		int size = Integer.parseInt((String)pagemap.get("size"));
		Query query = session.createQuery(hql)
				.setFirstResult((page-1)*size)
				.setMaxResults(size);
		List<?> list = query.list();

		return list;
	}

	@Override
	public int Count(Map<String, Object> pagemap) {
		Session session = sessionFactory.getCurrentSession();
		String hql = "select count(*) from User u where (u.status = '0' or u.status = '1') ";
		if(pagemap != null){
			if(pagemap.get("type") != null && !((String)pagemap.get("type")).equals("")){
				hql = hql + "and u.type = '"
						+(String)pagemap.get("type")+ "' ";
			}
			if(pagemap.get("userCode") != null && !((String)pagemap.get("userCode")).equals("")){
				hql = hql + "and u.userCode like '"
						+(String)pagemap.get("userCode")+ "%' ";
			}
			if(pagemap.get("userName") != null && !((String)pagemap.get("userName")).equals("")){
				hql = hql + "and u.userName like '"
						+(String)pagemap.get("userName")+ "%' ";
			}
			if(pagemap.get("mindate") != null && 
					!pagemap.get("mindate").equals("") && 
					pagemap.get("maxdate") != null && 
					!pagemap.get("maxdate").equals("")){
				hql = hql + "and u.createDate BETWEEN '"
						+ pagemap.get("mindate") +"' AND '"
						+ pagemap.get("maxdate") + "'";
			}
		}
		Query query = session.createQuery(hql);
	
		return ((Number) query.iterate().next())
	  	         .intValue();
	}

	@Override
	public void deleteUserByBranch(Long[] uids) {
		String hql = "";
		for(int i=0;i<uids.length;i++) {
            if(i==0) {
                hql = "id="+uids[i];
            } else {
                hql =hql + " or id="+uids[i];
            }
        }
		if(!hql.equals("")){
	        Session session= sessionFactory.getCurrentSession();
	        Query q= session.createQuery("update User u set u.status = '1' where "+hql);
	        q.executeUpdate();
			logger.info("批量删除用户" + uids);
		}
	}

}
